export interface IDialogBase {
    skinPrefab: IPrefab
}
import { IPrefab } from "../../config/PrefabPath";
import GuideManager from "../managers/GuideManager";
import { DialogEffect } from "./DialogEffect";

const { ccclass, property } = cc._decorator;
/**
 * 弹窗基类
 */
@ccclass
export default class DialogBase<Options = any> extends cc.Component {


    @property({ type: cc.Node, displayName: '背景遮罩' })
    public background: cc.Node = null;

    @property({ type: cc.Node, displayName: '弹窗主体' })
    public main: cc.Node = null;

    @property({ type: cc.Enum(DialogEffect.EffectType), displayName: "弹窗特效" })
    effect: DialogEffect.EffectType = DialogEffect.EffectType.MoveFromCenter;

    /**拦截点击事件 */
    private blocker: cc.Node = null;
    onLoad() {
        if (this.background) {
            if (!this.blocker) {
                this.blocker = new cc.Node('bg_mask_blocker');
                this.blocker.addComponent(cc.BlockInputEvents);
                this.blocker.setParent(this.background);
                this.blocker.setContentSize(this.background.getContentSize());
            }
            this.blocker.active = true;
        }
    }

    /** 弹窗选项 */
    protected options: Options = null;

    /** 弹窗流程结束回调（注意：该回调为 UIManager 专用，重写 hide 函数时记得调用该回调） */
    protected finishCallback: Function = null;

    /**
     * 弹窗已完全展示（子类请重写此函数以实现自定义逻辑）super.onShow()
     */
    protected onShow(): void {
    }

    /**
     * 弹窗已完全隐藏（子类请重写此函数以实现自定义逻辑）
     * @param force 是否被强制隐藏
     */
    protected onHide(force: boolean = false): void { }

    /**
     * 展示弹窗
     */
    public async show(options?: Options): Promise<void> {
        this.options = options || Object.create(null);
        this.node.active = true;
        if (this.background) {
            this.background.active = !!this.background;
        }
        if (this.main) {
            this.main.active = true;
        }
        this.init(this.options);
        this.updateDisplay(this.options);
        if (this.main) {
            await (DialogEffect[DialogEffect.getName(this.effect)]).show(this.main);
        } else {
            await (DialogEffect[DialogEffect.getName(this.effect)]).show(this.node);
        }
        this.onShow && this.onShow();
    }

    /**
     * 隐藏弹窗
     */
    public async hide(): Promise<void> {
        if (this.main) {
            await (DialogEffect[DialogEffect.getName(this.effect)]).hide(this.main);
        } else {
            await (DialogEffect[DialogEffect.getName(this.effect)]).hide(this.node);
        }
        this.blocker = null;
        this.node.active = false;
        if (this.background) {
            this.background.active = false;
        }
        this.onHide && this.onHide();
        this.finishCallback && this.finishCallback();
    }

    /**
     * 初始化（子类请重写此函数以实现自定义逻辑）
     */
    protected init(options: Options): void { }

    /**
     * 更新样式（子类请重写此函数以实现自定义样式）
     * @param options 弹窗选项
     */
    protected updateDisplay(options: Options): void { }

    /**
     * 设置弹窗完成回调（该回调为 UIManager 专用）
     * @param callback 回调
     */
    public setFinishCallback(callback: Function): void {
        this.finishCallback = callback;
    }

}
